package com.xiaoshuidi.cloud.framework.pay.core.client.impl.yinlian.miniapp;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.pay.core.client.PayCommonResult;
import com.xiaoshuidi.cloud.framework.pay.core.client.dto.*;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.dto.EnterpriseAccountEstablishReqDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.tongshangyun.dto.EnterpriseAccountEstablishRespDTO;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.yinlian.YinlianClientConfig;
import com.xiaoshuidi.cloud.framework.pay.core.client.impl.yinlian.YinlianCodeMapping;
import com.xiaoshuidi.cloud.framework.pay.core.enums.PayChannelEnum;
import com.xiaoshuidi.cloud.framework.pay.core.enums.PayDisplayModeEnum;
import com.xiaoshuidi.cloud.framework.pay.core.enums.PayFrameworkErrorCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

/**
 * 银联微信小程序支付接口
 */
@Slf4j
public class YinlianWXMiniAppPayClient extends AbstractYinlianMiniAppPayClient {

    public YinlianWXMiniAppPayClient(Long channelId, YinlianClientConfig config) {
        super(channelId, PayChannelEnum.YINLIAN_WX_MINIAPP.getCode(), config, new YinlianCodeMapping());
    }

    @SuppressWarnings("unchecked")
    @Override
    protected PayCommonResult<PayOrderUnifiedRespDTO<ObjectNode>> doUnifiedOrder(PayOrderUnifiedReqDTO reqDTO) throws Throwable {
        if (reqDTO.getChannelExtras() == null || reqDTO.getChannelExtras().get("appId") == null) {
            throw new ServiceException(PayFrameworkErrorCodeConstants.PAY_PARAM_MISSING);
        }
        if (reqDTO.getChannelExtras().get("openId") == null) {
            throw new ServiceException(PayFrameworkErrorCodeConstants.PAY_OPENID_ERROR);
        }
        ObjectNode json = baseParams(config.getMerchantCode(), config.getTerminalCode());
        if (StringUtils.hasText(reqDTO.getMerchantOrderId())) {
            json.put("merOrderId", reqDTO.getMerchantOrderId());
        } else {
            json.put("merOrderId", nextOrderNo());
        }
        json.put("instMid", "MINIDEFAULT");
        json.put("tradeType", "MINI");
        json.put("subAppId", reqDTO.getChannelExtras().get("appId"));
        json.put("subOpenId", reqDTO.getChannelExtras().get("openId"));
        json.put("totalAmount", reqDTO.getAmount());
        json.put("notifyUrl", reqDTO.getNotifyUrl());
        return call("/v1/netpay/wx/unified-order", json, (code, resp) -> new PayOrderUnifiedRespDTO<ObjectNode>()
                .setMerchantOrderId(json.get("merOrderId").asText())
                .setDisplayMode(PayDisplayModeEnum.FORM.getMode())
                .setDisplayContent(code == GlobalErrorCodeConstants.SUCCESS ? resp.get("miniPayRequest").toString() : null)
                .setRawRequest(json.toString())
                .setRawResponse(resp));
    }

}
